<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BannerController extends Controller
{
    public function _list(Request $request){

        $list = Banner::where([['id','>',0]])->get();

        return view('Banner.list',[
            'list' => $list
        ]);
    }

    public function edit(Request $request){
        $id = $request->filled('id') ? $request->id : null;

        if(!$_POST){
            $info = Banner::find($id);
            return view('Banner.edit', [
                'info' => $info
            ]);
        }

        $title = $request->filled('title') ? $request->title : null;
        $img = $request->filled('img') ? $request->img : null;
        $url = $request->filled('url') ? $request->url : '';
        $sort = $request->filled('sort') ? $request->sort : 100;//默认排序100
        $is_show = $request->filled('is_show') ? $request->is_show : 1;//默认展示1

        Banner::updateOrCreate(
            [
                'id' => $id
            ],
            [
                'title' => $title,
                'img' => $img,
                'url' => $url,
                'sort' => $sort,
                'is_show' => $is_show
            ]
        );

        return json_encode([
            'code' => 1,
            'msg' => 'success'
        ]);
    }

    public function del(Request $request){
        $id = $request->filled('id') ? $request->id : null;

        if(!$id)
            return json_encode([
                'code' => 0,
                'msg' => 'ID can not be null'
            ]);

        $id_arr = explode(',', $id);
        foreach($id_arr as $v){
            Banner::destroy($v);
        }

        return json_encode([
            'code' => 1,
            'msg' => 'success'
        ]);
    }
}
